package com.liming.resource.webapp.wicket.page;

import com.liming.core.web.wicket.BasePage;
import com.liming.resource.api.biz.IResourceService;
import com.liming.resource.api.persistence.IResourceBean;
import com.liming.resource.core.ResourceBeanImpl;
import org.apache.aries.blueprint.annotation.Reference;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.util.crypt.Base64;
import org.apache.wicket.util.lang.Bytes;
import org.lightcouch.Attachment;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类描述：
 * @创建人：sunlf
 * @创建时间：2014-03-29 下午1:49
 * @修改人：
 * @修改时间：
 * @修改备注：
 */

public class FilePage extends BasePage {

    @Named
    @Inject
    @Reference(id = "resourceService", serviceInterface = IResourceService.class)
    private IResourceService resourceService;

    private final FileUploadField file;
    Label label1 = new Label("pageName1", "上传文件");
    Label label2 = new Label("pageName2", "更新文件");
    final TextField formZipcode;

    public FilePage() {


        label1.setVisible(true);
        label2.setVisible(false);
        add(label1);
        add(label2);

        final Form<IResourceBean> form = new Form<IResourceBean>("uploadFileForm", new CompoundPropertyModel<IResourceBean>(new ResourceBeanImpl()));
        add(form);

        formZipcode = new TextField("describe");

        form.add(formZipcode);

        form.add(new AjaxButton("submit_button", form) {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> submitForm) {
                ResourceBeanImpl resourceBean = (ResourceBeanImpl) getParent().getDefaultModelObject();
                label1.setVisible(true);
                label2.setVisible(false);

                FileUpload upload = file.getFileUpload();
                if (upload != null) {
                    resourceBean.setDescribe(formZipcode.getValue());
                    resourceBean.setName(upload.getClientFileName());
                    resourceBean.setSize(Bytes.bytes(upload.getSize()).toString());
                    resourceBean.setAttachments(null);
                    Attachment attachment2 = new Attachment();
                    String data = Base64.encodeBase64String(upload.getBytes());
                    attachment2.setData(data);
                    resourceBean.addAttachment(upload.getClientFileName(), attachment2);
                    resourceService.putResource(resourceBean);
                } else {
                    resourceBean.setDescribe(formZipcode.getValue());
                    resourceService.putResource(resourceBean);
                }
                setResponsePage(FilePage.class);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                System.out.println("onError");
            }
        });

        add(new Link("reset_from_1") {
            @Override
            public void onClick() {
                form.setModelObject(new ResourceBeanImpl());
                label1.setVisible(true);
                label2.setVisible(false);
            }
        });

        form.add(new Link("reset_from_2") {
            @Override
            public void onClick() {
                form.setModelObject(new ResourceBeanImpl());
                label1.setVisible(true);
                label2.setVisible(false);
            }
        });

        form.add(file = new FileUploadField("file"));
        form.add(new UploadProgressBar("progress", form, file));


        List<IResourceBean> resourceList = resourceService.getAllResource();
        ListDataProvider<IResourceBean> listDataProvider = new ListDataProvider<IResourceBean>(resourceList);
        add(new DataView<IResourceBean>("fileRows", listDataProvider) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final Item<IResourceBean> item) {
                final IResourceBean file;
                file = item.getModelObject();

                item.add(new Label("nameData", file.getName()));
                item.add(new Label("describeData", file.getDescribe()));
                item.add(new Label("sizeData", file.getSize()));
                item.add(new Label("createDateData", file.getCreateDate()));
                item.add(new Label("version", file.getVersion()));
                item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
                    }
                }));

                item.add(new Link("edit") {
                    @Override
                    public void onClick() {
                        form.setModelObject((ResourceBeanImpl) getParent().getDefaultModelObject());
                        label1.setVisible(false);
                        label2.setVisible(true);
                    }
                });
               /* item.add(new Link("del") {
                    @Override
                    public void onClick() {
                        salesmanBeanService.deleteEntity(person.getId());
                        setResponsePage(SalesmanPage.class);

                    }
                });

                item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
                    }
                }));*/
            }
        });
    }
}
